<template>
  <page-header-wrapper>
    <a-card class="search-card  m-b-20" :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="14" :lg="12" :xl="8" :sm="24">
              <a-form-item label="登录账号">
                <a-input v-model="queryParam.strContent.LoginName" placeholder="请输入登录账号" @pressEnter="$refs.table.refresh(true)" />
              </a-form-item>
            </a-col>
            <!-- fix 暂时弃用 -->
            <!-- <a-col :md="8" :sm="24">
              <a-form-item label="用户名称">
                <a-input v-model="queryParam.strContent.UserName" placeholder="请输入用户名称"/>
              </a-form-item>
            </a-col> -->
            <a-col :md="10" :lg="8" :xl="8" :sm="24">
              <div class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="handleReset">重置</a-button>
              </div>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false">
      <s-table
        ref="table"
        :rowKey="record => record.UserID"
        :columns="columns"
        :data="loadData"
        :scroll="{x: true}"
        :pagination="{ showTotal: total => `共 ${total} 条` }"
      >
        <!-- 表格操作栏 -->
        <div slot="tabAction">
          <a-button v-action:add type="primary" icon="plus" @click="handleAdd">新增用户</a-button>
          <!-- fix: 导出示例 -->
          <!-- <a-button type="dashed" icon="export" @click="handleExportDownload">导出</a-button> -->
        </div>
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <span slot="status" slot-scope="text, record">
          <a-switch :key="text" :default-checked="text === '0' ? true : false" checked-children="启用" un-checked-children="停用" @change="checked=> handleChangeSwitch(checked, record)" />
        </span>
        <span slot="department" slot-scope="text">
          <ellipsis :length="20" tooltip>{{ text }}</ellipsis>
        </span>
        <span slot="date" slot-scope="text">{{ text | moment }}</span>

        <span slot="action" slot-scope="text, record">
          <template>
            <a v-action:edit @click="handleEdit(record)">修改<a-divider type="vertical" /></a>
            <a-popconfirm
              title="您确定是否重置当前用户密码?"
              ok-text="是"
              cancel-text="否"
              placement="bottomLeft"
              @confirm="handleResetPassword(record.UserID)"
            >
              <a-icon slot="icon" type="question-circle-o" />
              <a href="#">重置密码</a>
            </a-popconfirm>
          </template>
        </span>
      </s-table>
    </a-card>
    <edit-user v-if="visible" :visibles.sync="visible" :propsData="propsData"></edit-user>
  </page-header-wrapper>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import { getUserList, setUsers, resetPassword } from '@/api/system'
import { exportToExcel, formatJson } from '@/vendor/Export2Excel'
import EditUser from './edit'
const columns = [
  {
    title: '序号',
    align: 'center',
    width: '80px',
    scopedSlots: { customRender: 'serial' }
  },
  {
    title: '用户名称',
    align: 'center',
    dataIndex: 'UserName',
    width: '160px'
  },
  {
    title: '登录账号',
    align: 'center',
    dataIndex: 'LoginName'
  },
  {
    title: '联系电话',
    align: 'center',
    dataIndex: 'UserPhone'
  },
  {
    title: '所属部门',
    align: 'center',
    dataIndex: 'DeptName',
    scopedSlots: { customRender: 'department' }
  },
  {
    title: '登录次数',
    align: 'center',
    dataIndex: 'LoginCount',
    width: '150px'
  },
  {
    title: '是否启用',
    align: 'center',
    dataIndex: 'Mode',
    scopedSlots: { customRender: 'status' }
  },
  {
    title: '最后登录时间',
    align: 'center',
    dataIndex: 'LastLoginTime',
    width: '180px',
    scopedSlots: { customRender: 'date' }
  },
  {
    title: '操作',
    align: 'center',
    fixed: 'right',
    dataIndex: 'action',
    width: '150px',
    scopedSlots: { customRender: 'action' }
  }
]

export default {
  name: 'TableList',
  components: {
    STable,
    Ellipsis,
    EditUser
  },
  data () {
    this.columns = columns
    return {
      visible: false,
      // 查询参数
      queryParam: {
        strContent: {
          LoginName: ''
          // UserName: ''
        },
        userID: this.$store.getters.userid
      },
      propsData: {
        type: 'add',
        id: ''
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        return getUserList(requestParameters).then(res => {
          return res
        })
      }
    }
  },
  methods: {
    handleAdd () {
      this.visible = true
      this.propsData.type = 'add'
    },
    handleEdit (record) {
      this.visible = true
      this.propsData = {
        type: 'update',
        id: record.UserID
      }
    },
    handleReset () {
      this.queryParam = {
        strContent: {
          LoginName: ''
          // UserName: ''
        },
        userID: '1'
      }
      this.$refs.table.refresh(true)
    },
    handleChangeSwitch (checked, row) {
      setUsers({
        State: checked ? 'enable' : 'disenable',
        sys_users_Modules: { UserID: row.UserID }
      }).then((res) => {
        if (res.code === 200) {
          this.$message.success(`${checked ? '启用' : '停用'}成功`, 2.5)
          this.$refs.table.refresh(true)
        } else {
          this.$message.error(`${checked ? '启用' : '停用'}失败, 请稍后重试`, 2.5)
        }
      }).catch((err) => {
        this.$message.error(err, 2.5)
      })
    },
    handleResetPassword (UserID) {
      resetPassword({ UserID }).then(res => {
        this.$message.success('当前用户密码重置成功', 2.5)
      }).catch(() => {
        this.$message.error('当前用户密码重置失败, 请稍后重试', 2.5)
      })
    },
    handleExportDownload () {
      const multiHeader = [['Id', 'Main Information', '', '', 'Date']]
      const header = ['', 'Title', 'Author', 'Readings', '']
      const filterVal = ['id', 'title', 'author', 'pageviews', 'display_time']
      const list = []
      const data = formatJson(filterVal, list)
      const merges = ['A1:A2', 'B1:D1', 'E1:E2']
      const filename = '用户管理'
      exportToExcel({
        multiHeader,
        header,
        data,
        merges,
        filename
      })
    },
    handleRemove () {}
  }
}
</script>
